package cc.linker.steplink.crm.merchant.controller;


import cc.linker.steplink.crm.commons.model.PcUserInfo;
import cc.linker.steplink.crm.merchanth5.commons.UserInfoUtil;
import cc.linker.steplink.crm.merchant.service.PaymentSetService;
import cc.linker.steplink.crm.response.GlobalException;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 微信H5支付配置
 */

@Controller
@RequestMapping("payment")
public class PaymentSetController {

    @Autowired
    private PaymentSetService PaymentSetService;
    @Autowired
    private UserInfoUtil userInfoUtil;

    @Value("${wcpay.payurl}")
    private String payurl;

    @PostMapping("/set")
    @ResponseBody
    public void paymentSet(@RequestBody Map<String, Object> map, HttpServletRequest request) throws Exception {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        String payDescribe = "Steplink微信支付配置_" + orgId;
        String wcpayType = "STEPLINK_" + orgId;

        long time = Calendar.getInstance().getTimeInMillis();
        Map<String, String> paramsMap = new HashMap<>();

        String wcAppId = PaymentSetService.getAppIdByOrgId(orgId);

        if (StringUtils.isEmpty(wcAppId)) {
            throw GlobalException.build("请先绑定公众号");
        }

        if (StringUtils.isEmpty(map.get("wcApikey"))
                || StringUtils.isEmpty(map.get("wcApimch"))
                || StringUtils.isEmpty(map.get("wcAppsecret"))
        ) {
            throw GlobalException.build("参数错误");
        }

        paramsMap.put("payDescribe", payDescribe);
        paramsMap.put("wcApikey", map.get("wcApikey").toString());
        paramsMap.put("wcApimch", map.get("wcApimch").toString());
        paramsMap.put("wcAppId", wcAppId);
        paramsMap.put("wcAppsecret", map.get("wcAppsecret").toString());
        paramsMap.put("wcpayType", wcpayType);
        paramsMap.put("timestamp", String.valueOf(time));

        String appSign = ParamSignUtils.sign(paramsMap, null).get("appSign");
        List<NameValuePair> list = new ArrayList<NameValuePair>();
        list.add(new BasicNameValuePair("payDescribe", payDescribe));
        list.add(new BasicNameValuePair("wcApikey", map.get("wcApikey").toString()));
        list.add(new BasicNameValuePair("wcApimch", map.get("wcApimch").toString()));
        list.add(new BasicNameValuePair("wcAppId", wcAppId));
        list.add(new BasicNameValuePair("wcAppsecret", map.get("wcAppsecret").toString()));
        list.add(new BasicNameValuePair("wcpayType", wcpayType));
        list.add(new BasicNameValuePair("timestamp", String.valueOf(time)));
        list.add(new BasicNameValuePair("appSign", appSign));
        String ret = ParamSignUtils.callService(payurl + "/pay/wcpay/generateWcpayInfo", list, 3000, 4000);
        if (!"".equals(ret)) {
            HashMap<String, Object> info = PaymentSetService.getinfo(orgId);
            paramsMap.put("orgId", orgId);
            if (null != info && !info.isEmpty()) {
                PaymentSetService.edit(paramsMap);
            } else {
                PaymentSetService.add(paramsMap);
            }
        }
    }


    @PostMapping("/getinfo")
    @ResponseBody
    public Map getInfo(HttpServletRequest request) throws Exception {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        return PaymentSetService.getinfo(orgId);
    }


}
